package com.hermit.qmst.request;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpEngine {
	protected static final String ACTIVITY_TAG = "HBServerConn";
	public static final String DOWNLOADFILE = "DOWNLOADFILE";
	private static String ERROR = "<?xml";
	public static final String GET = "GET";
	public static final String HTTP = "http://";
	public static final int MAX_TIME_OUT_COUNT = 3;
	public static final int MAX_TIME_OUT_TIME = 5000;
	public static final String POST = "POST";
	public static final String UPLOADFILE = "UPLOADFILE";
	private HttpURLConnection connect;
	private String postContent;
	private String requestType;
	private String requestURL;
		
	public HttpEngine(){
	
	}
	
	public HttpEngine(String pRequestType, String pUrl,String pPostContent){
		init(pRequestType, pUrl, pPostContent);
	}
	
	private void init(String pRequestType, String pUrl,String pPostContent){
		requestType = pRequestType;
		requestURL = pUrl;
		postContent = pPostContent;
		
	} 
	
	public String requestPost(){
		
		try {
			URL url = new URL(requestURL);
			connect = (HttpURLConnection) url.openConnection();
			connect.setRequestMethod("POST");
			connect.setConnectTimeout(60000);
			connect.setRequestProperty("connection", "keep-alive");
			connect.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");	
			connect.setRequestProperty("Content-Length", String.valueOf(postContent.getBytes().length));
			connect.setDoOutput(true);
			connect.setDoInput(true);  
			connect.setUseCaches(false);
			
			connect.connect();

			OutputStream _OutputStream = connect.getOutputStream();
			_OutputStream.write(postContent.getBytes("utf-8"));
			_OutputStream.flush();
			_OutputStream.close();
			
			if (HttpURLConnection.HTTP_OK == connect.getResponseCode()) {
				InputStream is = connect.getInputStream();
				String encod = connect.getContentEncoding();
				String type = connect.getContentType();
				String currentLine = "";
				StringBuilder returnContent = new StringBuilder();
		
				BufferedReader reader = new BufferedReader(new InputStreamReader(is));
				while ((currentLine = reader.readLine()) != null) {
					if (currentLine.length() > 0) {
						returnContent.append(currentLine);
					}
					
				}
				reader.close();
				connect.disconnect();
				return returnContent.toString();
			}
			
			   
		} catch (IOException e) {
			
			e.printStackTrace();
			connect.disconnect();
		}finally{
			if (connect != null) {
				connect.disconnect();
			}
			
		}
		
		return null;
	}
	
public String requestGet(){
		
		try {
			URL url = new URL(requestURL);
			connect = (HttpURLConnection) url.openConnection();
			connect.setRequestMethod("GET");
			connect.setConnectTimeout(60000);
			connect.setRequestProperty("connection", "keep-alive");
			connect.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			//connect.setRequestProperty("Content-Length", String.valueOf(postContent.getBytes().length));
		
			connect.setDoOutput(true);
			connect.setDoInput(true);
			  
			connect.setUseCaches(false);
			connect.connect();
			   
		      

			if (HttpURLConnection.HTTP_OK == connect.getResponseCode()) {
				InputStream is = connect.getInputStream();
			
				String type = connect.getContentType();
				String currentLine = "";
				StringBuilder returnContent = new StringBuilder();
				BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
				while ((currentLine = reader.readLine()) != null) {
					if (currentLine.length() > 0) {
						returnContent.append(currentLine);
					}
					
				}
				reader.close();
				connect.disconnect();
				return returnContent.toString();
			}
			
			   
		} catch (IOException e) {
			
			e.printStackTrace();
			connect.disconnect();
		}finally{
			connect.disconnect();
		}
		
		return "";
	}
	
}
